package com.yebxxx.server.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yebxxx.server.mapper.EmployeeMapper;
import com.yebxxx.server.mapper.MailLogMapper;
import com.yebxxx.server.pojo.*;
import com.yebxxx.server.service.IEmployeeService;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author stx
 * @since 2022-03-23
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private MailLogMapper mailLogMapper;
    /**
     * 获取所有员工，分页查询
     * @param currentPage
     * @param size
     * @param employee
     * @param beginDateScope
     * @return
     */
    @Override
    public PageBean getEmployeeByPage(Integer currentPage, Integer size, Employee employee, LocalDate[] beginDateScope) {
        Page<Employee> page=new Page<>(currentPage,size);
        IPage<Employee> employeeByPage = employeeMapper.getEmployeeByPage(page, employee, beginDateScope);
        PageBean pageBean=new PageBean(employeeByPage.getTotal(),employeeByPage.getRecords());
        return pageBean;
    }

    /**
     * 获取最大工号
     * @return
     */
    @Override
    public RespBean maxWorkId() {
        List<Map<String, Object>> maps = employeeMapper.selectMaps(new QueryWrapper<Employee>().select("max(workId)"));
        String s = maps.get(0).get("max(workId)").toString();
        System.out.println(s);
        int i = Integer.parseInt(s);
        int id=i+1;
        System.out.println(id);
        String maxId = String.format("%08d", id);
//        System.out.println(format);

        return RespBean.success(null,maxId);
    }

    /**
     * 添加员工
     * @param employee
     * @return
     */
    @Override
    public RespBean addEmp(Employee employee) {
        //处理合同期限保留两位小数
        LocalDate beginContract = employee.getBeginContract();
        LocalDate endContract = employee.getEndContract();
        long days = beginContract.until(endContract, ChronoUnit.DAYS);
        DecimalFormat decimalFormat=new DecimalFormat("##.00");

        employee.setContractTerm(Double.parseDouble(decimalFormat.format(days/365.00)));
        if(1==employeeMapper.insert(employee)){
            Employee emp=employeeMapper.getEmployee(employee.getId()).get(0);
            //数据库记录发送的消息
            String msgId = UUID.randomUUID().toString();
            MailLog mailLog=new MailLog();
             mailLog.setMsgId(msgId);
             mailLog.setEid(employee.getId());
             mailLog.setStatus(0);
             mailLog.setRouteKey(MailConstance.MAIL_ROUTEING_KEY_NAME);
             mailLog.setExchange(MailConstance.MAIL_EXCHANGE);
             mailLog.setCount(0);
             mailLog.setTryTime(LocalDateTime.now().plusMinutes(MailConstance.MSG_TIMEOUT));
             mailLog.setCreateTime(LocalDateTime.now());
             mailLog.setUpdateTime(LocalDateTime.now());
             mailLogMapper.insert(mailLog);

             //发送消息
             rabbitTemplate.convertAndSend(MailConstance.MAIL_EXCHANGE,MailConstance.MAIL_ROUTEING_KEY_NAME,emp,new CorrelationData(msgId));
             return  RespBean.success("添加成功！");
        }
        return RespBean.error("添加失败！");

    }

    /**
     * 查询员工
     * @param id
     * @return
     */
    @Override
    public List<Employee> getEmployee(Integer id) {
        return employeeMapper.getEmployee(id);
    }
    /**
     * 获取所有员工工资账套
     * @param currentPage
     * @param size
     * @return
     */
    @Override
    public PageBean getEmployeeWithSalary(Integer currentPage, Integer size) {
        Page<Employee> page=new Page<>(currentPage,size); //开启分页
        IPage<Employee> employeeIPage=employeeMapper.getEmployeeWithSalary(page);
        PageBean pageBean=new PageBean(employeeIPage.getTotal(),employeeIPage.getRecords());
        return pageBean;
    }

    /**
     * 员工离职
     * @param
     * @param
     * @return
     */
    @Override
   public RespBean updateWorkState(Employee employee) {
        Integer i=employeeMapper.updateWorkState(employee);
      if(i==1){
          return  RespBean.success("离职成功");
      }
       return  RespBean.success("离职失败");
    }




    @Override
    public PageBean deleteEmList(Integer currentPage, Integer size, EmployeeTrain employeeTrain, Teacher teacher) {
        Page<EmployeeTrain> page=new Page<>(currentPage,size);
        IPage<EmployeeTrain> employeeIPage=employeeMapper.deleteEmps(page);
        PageBean pageBean=new PageBean(employeeIPage.getTotal(),employeeIPage.getRecords());
        return pageBean;

    }

    /**
     * 更新员工部门
     * @param workId
     * @param depId
     */
    @Override
    public void updateEmpDep(String workId, Integer depId) {
        employeeMapper.updateEmpDep(workId, depId);
    }
}
